exports.bulkPurchase = function (req, res) {

    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    var skip  =  restfulParams.skip == undefined  ? 0 : restfulParams.skip;
    var limit =  restfulParams.limit == undefined ? config.publicConfig.LIMIT : restfulParams.limit;


    //任务流创建
    async.waterfall([
            
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchBulkPurchaseActivityUserInfo + "/bpau_uid/" + restfulParams.u_id + "/skip/"+skip+"/limit/" + limit + "/sort_order/bpau_join_time-desc",
                        method: 'POST'
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData = data.data;
                        cb();
                    }
                );

            },

            //如果为参与的拼团，查询还差几人
            function(cb){

                //创建添加任务
                var tasks = [];

                //循环节点列表
                resultData.forEach(function (item, i) {

                //创建每一个插入任务
                tasks.push(function (cb) {

                    //判断是否为团长,并且并未完成拼团
                    if(item.bpau_fid == 0){
                        
                        var diff = item.bpau_member_count - item.bpau_user_count;
                        item.diffUserCount = diff;

                        cb();

                    //不是团长    
                    }else{

             
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.searchBulkPurchaseActivityUserInfo + "/bpau_id/" + item.bpau_fid,
                                    method: 'POST'
                                },
                                function (err, data) {
    
                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }
    
                                    var nowBulkData = data.data[0];
    
                                    if(nowBulkData.bpau_fid == 0){
                                        
                                        var diff = nowBulkData.bpau_member_count - nowBulkData.bpau_user_count;
                                    
                                        item.diffUserCount = diff;

                                      
                                        cb();
                                    }
                                }
                            );
                    }

                });

            });

            async.waterfall(tasks, cb);

            }

        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}